package com.aaa.whj.controller;


import com.aaa.whj.entity.Tab_scl_record;
import com.aaa.whj.entity.Tab_topic;
import com.aaa.whj.service.IRecordService;

import com.aaa.whj.service.impl.RecordServiceImpl;

import com.util.LayData;
import com.util.PrintJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "record", value = "/record")
public class RecordServlet extends HttpServlet {
    private final IRecordService recordService=new RecordServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        if(method.equals("findAll")){
            record_query(req,resp);
        }else if(method.equals("topic")){
            topic(req,resp);
        }

    }

    private void topic(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        List<Tab_topic> topics=recordService.queryTopic(id);
        long count = recordService.count(null);

        LayData success = LayData.success((int) count,topics);
        PrintJsonData.printData(resp,success);


    }

    private void record_query(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page");
        System.out.println("page = " + page);
        String limit = req.getParameter("limit");
        System.out.println("limit = " + limit);
        List<Tab_scl_record> tab_scl_records = recordService.queryAll(page, limit);
        System.out.println("tab_scl_records = " + tab_scl_records);

        long count = recordService.count(null);
        System.out.println("count = " + count);

        LayData success = LayData.success((int) count,tab_scl_records);
        PrintJsonData.printData(resp,success);
    }
}
